//
//  AppDelegate.m
//  JLCLSDK_Minus_Demo
//
//  Created by jlcl on 2024/9/24.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
@interface AppDelegate ()<JLCLSplashAdDelegate>
@property (nonatomic, strong) JLCLAdManagementCenter *aCenter;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *rootViewController = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    [[JLCLSDKConfig defaultInstance] setAppID:kJLCLTestAppID callback:^(BOOL isSuccessful) {
        if (isSuccessful) {
            NSLog(@"成功");
            if (@available(iOS 14, *)) {//  iOS14开始必须进行授权
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
//                    [self adShow];
                    if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                        NSLog(@"已获取权限");
                    }else {
                        NSLog(@"未获取权限");
                    }
                }];
            } else {
//                [self adShow];
            }
        }else {
            NSLog(@"失败");
        }
    }];
    NSLog(@"聚量磁力聚合广告SDK当前版本：%@",[[JLCLSDKConfig defaultInstance] getVersion]);
    return YES;
}


- (void)adShow {
    //进入主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        self.aCenter = [[JLCLAdManagementCenter alloc] init];
        JLCLSplashAdData *data = [[JLCLSplashAdData alloc] init];
        data.ad_id = kJLCLTestSplashAdID;// 广告位ID   必传
        data.location = 1;//位置标识符 多个同类型广告时请准确填写位置标识 默认为1
        data.timeoutInterval = 5;/// 开屏广告超时时间，默认为3s，当设置时间小于3秒时，使用默认值3秒
        data.delegate = self;
        data.rootViewController = self.window.rootViewController;
        [self.aCenter jlcl_showSplashAd:data];
    });
}

- (void)jlcl_splashAdDidLoad {
    NSLog(@"开屏广告加载完成");
}

- (void)jlcl_splashAdDidClick {
    NSLog(@"开屏广告被点击");
}

- (void)jlcl_splashAdDidLoadFailWithError:(NSString *)error {
    NSLog(@"开屏广告加载失败 = %@",error);
    
}

- (void)jlcl_splashAdDidClickSkipOrClose {
    NSLog(@"开屏广告已关闭");
    [self.aCenter jlcl_closeSplashAd];
    self.aCenter = nil;
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

-(void)applicationDidBecomeActive:(UIApplication *)application
{
    if (@available(iOS 14.5, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
        }];
    }
}
@end
